package com.ctv.my.customview.BaiajCustomView.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.CollapsibleActionView;
import android.view.View;

/**
 * Created by baiaj on 2017/8/1.
 */

public class RadarView extends View {

    private static final int LEVEL = 5;
    private static final String TEXT_COLOR = "#4a4a4a";
    private static final String VALUE_COLOR = "#6495ED";
    private static final int TEXT_OFFSET = 5;
    private static final int VALUE_ALPHA = 127;
    private static final int TEXT_SIZE = 26;

    private Paint mPaint;
    private Paint textPaint;
    private Paint valuePaint;
    private int mWidth;
    private int mHeight;
    private int xRadius, yRadius;
    private int firstPointX, firstPointY;
    private String textColor = TEXT_COLOR;
    private String valueColor = VALUE_COLOR;
    private int titleSize = TEXT_SIZE;
    private String[] titles = new String[]{"a", "b", "c", "d", "e", "f"};
    private double[] datas = new double[]{60, 100, 50, 100, 60, 60};

    public RadarView(Context context) {
        super(context);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();                   // 创建画笔
        mPaint.setColor(Color.parseColor("#CCCCCC"));           // 画笔颜色 - 灰色
        mPaint.setStyle(Paint.Style.STROKE);    // 填充模式 - 描边
        mPaint.setStrokeWidth(3);              // 边框宽度 - 10

        textPaint = new Paint();
        textPaint.setColor(Color.parseColor(textColor));
        textPaint.setTextSize(titleSize);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);

        valuePaint = new Paint();
        valuePaint.setColor(Color.parseColor(valueColor));
        valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        valuePaint.setAlpha(VALUE_ALPHA);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        xRadius = mWidth / (LEVEL + 1) / 2;
        yRadius = (int) Math.sqrt((xRadius * xRadius - (xRadius / 2) * (xRadius / 2)));
        firstPointX = mWidth / 2 - xRadius;
        firstPointY = mHeight / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path path = new Path();
        // 绘制背景
        drawBg(canvas, path);
        // 绘制文字
        drawText(canvas);
        // 绘制内容
        drawContent(canvas, path);

    }

    // region 绘制方法
    private void drawBg(Canvas canvas, Path path) {
        // 绘制基本5个六边形
        for (int i = 0; i < LEVEL; i++) {
            path.reset();
            path.moveTo(firstPointX - xRadius * i, firstPointY);
            path.lineTo(firstPointX - xRadius * i + xRadius / 2 * (i + 1), firstPointY - yRadius * (i + 1));
            path.lineTo(mWidth / 2 + xRadius / 2 * (i + 1), firstPointY - yRadius * (i + 1));
            path.lineTo(mWidth / 2 + xRadius * (i + 1), firstPointY);
            path.lineTo(mWidth / 2 + xRadius / 2 * (i + 1), firstPointY + yRadius * (i + 1));
            path.lineTo(firstPointX - xRadius * i + xRadius / 2 * (i + 1), firstPointY + yRadius * (i + 1));
            path.close();
            canvas.drawPath(path, mPaint);
        }
        // 绘制中心到末端的直线
        canvas.drawLine(xRadius, mHeight / 2, mWidth - xRadius, mHeight / 2, mPaint);
        canvas.drawLine(xRadius + xRadius * LEVEL / 2, mHeight / 2 - yRadius * LEVEL, mWidth / 2 + xRadius * LEVEL / 2, mHeight / 2 + yRadius * LEVEL, mPaint);
        canvas.drawLine(xRadius + xRadius * LEVEL / 2, mHeight / 2 + yRadius * LEVEL, mWidth / 2 + xRadius * LEVEL / 2, mHeight / 2 - yRadius * LEVEL, mPaint);
    }

    private void drawText(Canvas canvas) {
        Paint.FontMetrics metrics = textPaint.getFontMetrics();
        // 标题高度
        float textHeight = metrics.descent - metrics.ascent;
        // 计算每个标题的宽度
        float aWidth = textPaint.measureText(titles[0]);
        float bWidth = textPaint.measureText(titles[1]);
        float cWidth = textPaint.measureText(titles[2]);
        float dWidth = textPaint.measureText(titles[3]);
        float eWidth = textPaint.measureText(titles[4]);
        float fWidth = textPaint.measureText(titles[5]);
        canvas.drawText(titles[0], firstPointX - xRadius * 4 - aWidth - TEXT_OFFSET * 2, firstPointY + textHeight / 2, textPaint);
        canvas.drawText(titles[1], firstPointX - xRadius * 4 + xRadius / 2 * 5 - bWidth / 2, firstPointY - yRadius * 5 - textHeight / 2, textPaint);
        canvas.drawText(titles[2], mWidth / 2 + xRadius / 2 * 5 - cWidth / 2, firstPointY - yRadius * 5 - textHeight / 2, textPaint);
        canvas.drawText(titles[3], mWidth / 2 + xRadius * 5 + TEXT_OFFSET, firstPointY + textHeight / 2, textPaint);
        canvas.drawText(titles[4], mWidth / 2 + xRadius / 2 * 5 - eWidth / 2, firstPointY + yRadius * 5 + textHeight, textPaint);
        canvas.drawText(titles[5], firstPointX - xRadius * 4 + xRadius / 2 * 5 - fWidth / 2, firstPointY + yRadius * 5 + textHeight, textPaint);
    }

    private void drawContent(Canvas canvas, Path path) {
        path.reset();
        // 边长
        int lineWidth = xRadius * LEVEL;
        for (int j = 0; j < datas.length; j++) {
            float precent = (float) (datas[j] / 100);
            float cosValue = (float) Math.cos(60 * j * Math.PI / 180);
            float sinValue = (float) Math.sin(60 * j * Math.PI / 180);
            if (j == 0) {
                path.moveTo(mWidth / 2 - lineWidth * precent * cosValue, mHeight / 2 - lineWidth * precent * sinValue);
            } else {
                path.lineTo(mWidth / 2 - lineWidth * precent * cosValue, mHeight / 2 - lineWidth * precent * sinValue);
            }
        }
        path.close();
        canvas.drawPath(path, valuePaint);
    }
    // endregion

    // region 公开方法

    /**
     * 设置标题
     *
     * @param titles 每个值对应的标题，和数据对应
     */
    public void setTitles(String[] titles) {
        this.titles = titles;
    }

    /**
     * 设置每个标题对应的数据
     *
     * @param datas 数值(0~100)
     */
    public void setDatas(double[] datas) {
        this.datas = datas;
    }

    /**
     * 设置标题字体颜色
     *
     * @param color 颜色值
     */
    public void setTitleColor(String color) {
        this.textColor = color;
        textPaint.setColor(Color.parseColor(textColor));
    }

    /**
     * 设置内容的颜色
     *
     * @param color 颜色值
     */
    public void setContentColor(String color) {
        this.valueColor = color;
        valuePaint.setColor(Color.parseColor(valueColor));
    }

    public void setTitleSize(int size) {
        this.titleSize = size;
        textPaint.setTextSize(titleSize);
    }

    // endregion
}
